//--------------毛重和净重校验提醒
function adjustGrossAndNet() {
    var mz = $("#mz").attr("value");
    var jz = $("#jz").attr("value");
    if (mz && jz) {
        if ((mz - jz ) > (jz * 0.3)) {
            $.messager.alert('提示', "毛重多于净重的30%");

        }
    }
}

//-----------------------------运费、保费、杂费币制下拉框隐藏
function bzChange(obj, id) {
    try {
        clearSelectValue($("#" + id));
        if (obj.value == 1) {
            $("#" + id).combogrid("readonly", true);
        } else {
            $("#" + id).combogrid("readonly", false);
        }

        obj.focus();
    } catch (e) {

    }

}

function changeSpacialRelation() {
    var selectValue = $("#specialShip").val();
    var priceEffice = $("#priceEffice");
    if (selectValue == "1") {
        priceEffice.combobox("enable");
    } else {
        priceEffice.combobox('setValue', '0').combobox("disable");
        $("[name='baHgbaseinfo.priceEffect']").removeAttr("disabled");
    }
}

function bzChangeEval(objId, id) {
    bzChange($("#" + objId)[0], id);
    focusDemo($("#" + objId));
}

function copyHgNewOne() {
    var gjh = $("#GJH").attr("value");
    $("#baHgbaseinfoId").val("");
    $("#GJH").val("");
    $("#GJGLMM").val("");
    $("#TYBH").val("");
    $("#ZT").val("");
    $("#HGBH").val("");
    $("#SBRQ").val("");
    $("#HGISLOCK").val("");
    changeHgStatuName("");
    $("input[id^='hggoods_id_']").each(function () {
        $(this).attr("value", "");
    });
    $("input[id^='hggoods_hwxh_']").each(function () {
        $(this).attr("value", "");
    });
    $("input[id^='box_id_']").each(function () {
        $(this).attr("value", "");
    });

    try {
        //设置 海关经营单位
        selfCustom(operatorCustomsTradeCode, operatorCompanyName);
    } catch (e) {
        // TODO: handle exception
    }

    if (gjh && gjh != "") {
        resetIframe("复制" + gjh);
    }
}

function setHgReturnGoodListValue(goodList) {
    var tmp;
    for (var index = 0; index < goodList.length; index++) {
        tmp = goodList[index];
        $("#hggoods_id_" + tmp.index).attr("value", tmp.id);
        $("#hggoods_hwxh_" + tmp.index).attr("value", tmp.spxh);
    }

}

function setHgReturnBoxListValue(boxList) {
    var tmp;
    for (var index = 0; index < boxList.length; index++) {
        tmp = boxList[index];
        $("#box_id_" + tmp.index).attr("value", tmp.id);
    }

}

function caclulateHgGoodPrice() {
    caculatePriceBase("hggoods_zj", "hggoods_sbsl", "hggoods_dj");
}

function editSHowHgGoodsCombogridSelect(trSerNo) {
    setSelectValueHGGood($("#hggoodsSelect_spbh"), "hggoods_spbh", "hggoods_spbhdm", trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_sbjldw"), "hggoods_sbjldw", "hggoods_sbjldwName",
                         trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_bz"), "hggoods_bz", "hggoods_bzName", trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_dyjldw"), "hggoods_dyjldw", "hggoods_dyjldwName",
                         trSerNo);
    // setSelectValueHGGood($("#hggoodsSelect_yt"), "hggoods_yt", "hggoods_ytName", trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_destinationCountry"), "hggoods_destinationCountry",
                         "hggoods_destinationCountryName", trSerNo);

    setSelectValueHGGood($("#hggoodsSelect_dejldw"), "hggoods_dejldw", "hggoods_dejldwName",
                         trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_ycgmdg"), "hggoods_ycgmdg", "hggoods_ycgmdgName",
                         trSerNo);
    setSelectValueHGGood($("#hggoodsSelect_zm"), "hggoods_zm", "hggoods_zmName", trSerNo);
}
function setSelectValueHGGood(selectObje, valueHiddenId, textHiddenId, trSerNo) {
    setSelectValue(selectObje, valueHiddenId, textHiddenId, valueHiddenId + "_" + trSerNo,
                   textHiddenId + "_" + trSerNo);
}
/*
 function setSelectValue(selectObje, valueHiddenId, textHiddenId, baseValueId, baseTextId) {
 var value = $("#" + baseValueId).val();
 var text = $("#" + baseTextId).val();
 setSelectValueBase(selectObje, value, text);
 }

 function setSelectValueBase(selectObje, value, text) {
 // clearSelectValue(selectObje);
 initSelectTextAndValue(selectObje, text, value);
 }*/

function setHgReturnValue(baHgbaseinfo) {
    $('#baHgbaseinfoId').val(baHgbaseinfo.id);
    $('#GJH').val(baHgbaseinfo.gjh);
    $('#GJGLMM').val(baHgbaseinfo.gjglmm);
    $('#TYBH').val(baHgbaseinfo.tybh);
    $('#HGBH').val(baHgbaseinfo.hgbh);
    $('#ZT').val(baHgbaseinfo.zt);
    $('#SBRQ').val(baHgbaseinfo.sbrq);
    $('#HGISLOCK').val(baHgbaseinfo.islock);
    $('#HGVERSION_NO').val(baHgbaseinfo.version_no);
    changeHgStatuName(baHgbaseinfo.state);
}

function validateHgGood() {
    return validateBase("hgGoodRequired");
}

//--------------添加商品
var hggoodsSerIdIndex = 1;
function setGoodsSpxhId(hggoodsSerIdIndex) {
    $("#hggoods_spxh").val(hggoodsSerIdIndex);
}
setGoodsSpxhId(hggoodsSerIdIndex);

function copyHgGood() {
    setGoodsSpxhId(hggoodsSerIdIndex);
    clearHgTrSuccess();
    hggoods_edit_serid = "";
    $("#hggoods_addButton").val("添加");
    $("#hggoods_id").attr("value", "");
    $("#hggoods_hwxh").attr("value", "");
    reSetShowSerid();
}
function clearHgTrSuccess() {
    $("#goodshow_serid_" + hggoods_edit_serid).parent().parent().removeClass("success");
}

var hggoods_edit_serid = "";

function addOrUpdateHggoods() {
    if (validateHgGood()) {

    } else {
        return;
    }
    if (hggoods_edit_serid != null && hggoods_edit_serid != "") {
        updateHggoodsTr(hggoods_edit_serid);
    } else {
        addHggoods();
    }
    reSetShowSerid();
}
function addHggoods() {
    var addHtml = getNewHggoodsHtml();
    hggoodsSerIdIndex++;
    $("#hggoods_lastTR").before(addHtml);
    clearHgGoodInput();

    setGoodsSpxhId(hggoodsSerIdIndex);

    $("#hggoods_htspxmxh").focus();
    clearHgDejldwRequredClass();
    $("#hggoods_spggxh").attr("value", "0");
    noticesHgGoodCounts();
}

function noticesHgGoodCounts() {
    var result = 0;
    var inputs = $("input[id^='goodshow_serid_']");
    var maxCount = 20;
    if (inputs.length > maxCount) {
        $.messager.alert('提示', "报关单表体超过" + maxCount + "项商品，如涉及到联网核销证件，将可能被退单！");
    }
}

// function editHggoodsTr(trSerNo) {
//     hggoods_edit_serid = "" + trSerNo;
//     editSHowHgGoodsCombogridSelect(trSerNo);
//     editTr("hggoods_TR" + trSerNo);
//     $("#hggoods_addButton").val("修改");
//     $("#hggoods_htspxmxh").focus();
//     var hsbmc = $("#hggoods_spbhdm").attr("value");
//     if (hsbmc) {
//         setHgDejldwRequired(hsbmc);
//     }
// }

function editHggoodsTr(trSerNo) {
    hggoods_edit_serid = "" + trSerNo;
    $.each($("#hggoods_input_table input"), function (i, n) {
        $(n).val("");
    });
    var inputs = $("#hggoods_TR" + trSerNo + " input");
    $.each(inputs, function (i, n) {
        var id = $(n).attr("id");
        if (id) {
            id = id.substr(0, id.indexOf("_" + trSerNo));
            $("#" + id).val($(n).val());
            $("#" + id + "_show").val($(n).val());
            var comb = $($("#" + id).prev().children()[1]);
            if (comb.length != 0 && comb != undefined && comb.attr("autocomplete") != undefined) {
                comb.val($(this).val());
            }
        }
    });
    editTr("hggoods_TR" + trSerNo);
    $("#hggoods_addButton").val("修改");
    $("#hggoods_htspxmxh").focus();

}

function clearHgGoodInput() {
    $("#hggoods_input_table input[type!='button']").each(
        function (index, element) {
            if (index >= 0) {
                if ($(element).attr("selectData")) {

                    if ($(element).attr("isInit") && $(element).attr("isInit") == 1) {
                        $(element).combogrid("clear");
                    } else {
                        $(element).attr("value", "");
                    }

                    //	$(element).combogrid("clear");
                    //initSelectTextAndValue($(element),"","");
                } else {
                    $(element).attr("value", "");
                }
            }
        });
    $("#hggoods_input_table select").each(
        function (index, element) {
            $(element).val("");
        });
}

function updateHggoodsTr() {
    updateTr("hggoods_input_table", hggoods_edit_serid);

    clearHgGoodInput();
    setGoodsSpxhId(hggoodsSerIdIndex);
    $("#hggoods_htspxmxh").focus();
    initHggoodsInput();
    clearHgDejldwRequredClass();
    var hgGoodtable = $("#hgGoodtable");
    validateGoodsTable("hgGoodtable");
    /*
     $("#hgGoodtable input").each(function(index, element) {
     var input = $(this);
     $("#myForm").validate().element(input);
     });*/

    $("#hggoods_spggxh").attr("value", "0");
}

function initHggoodsInput() {
    setGoodsSpxhId(hggoodsSerIdIndex);
    hggoods_edit_serid = "";
    $("#hggoods_addButton").val("添加");
    initHgDestinationCountry();
}

function getNewHggoodsHtml() {
    if (boxSerIdIndex == 0) {
        boxSerIdIndex = $("#JZX-table tr>td:first-child input[id*='id']").length;
    }
    var idPrefixName = "hggoods";
    var result = "<tr id='" + idPrefixName + "_TR" + hggoodsSerIdIndex + "' onclick='editHggoodsTr("
                 + hggoodsSerIdIndex + ")'>";
    var namePre = "commodities[" + hggoodsSerIdIndex + "].";
    result += addTr("hggoods_input_table", hggoodsSerIdIndex, namePre);
    result +=
        "<td><img src='../../cargodecl/images/delete.gif' onclick='delHggoodsTr(\"" + idPrefixName
        + "_TR"
        + hggoodsSerIdIndex + "\")'/></td>";
    result += "</tr>";
    return result;
}

function delHggoodsTr(trId) {
    $("#" + trId).remove();
    clearHgGoodInput();
    initHggoodsInput();
    $("#hggoods_spxh").focus();
    reSetShowSerid();
    reSetArrNum();
    if (event.stopPropagation) {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }
}
function reSetArrNum() {
    $.each($("#hgGoodtable tr:gt(0)"), function (i, n) {
        var trNo = $(n).find("td:eq(0) input[id^='goodshow_serid_']").val() - 1;//表格序号列的值-1
        var trId = $(n).attr("id");
        var trIdNum = trId.substr(10);//表明tr是第几行
        if (trNo == trIdNum) {
            return;
        }
        $(n).attr("id", trId.substr(0, 10) + trNo);
        $(n).attr("onclick", "editHggoodsTr(" + trNo + ")");
        $(n).find("img").attr("onclick", "delHggoodsTr('hggoods_TR" + trNo + "')");
        $.each($(n).find("input"), function (j, m) {
            var id = $(m).attr("id");
            if (id) {
                $(m).attr("id", id.substr(0, id.lastIndexOf("_") + 1) + trNo);
            }
            var name = $(m).attr("name");
            if (name) {
                $(m).attr("name",
                          name.substr(0, name.indexOf("[") + 1) + trNo + name.substr(
                              name.indexOf("]")));
            }
        });
    });
}
function selectAddSelectRequired() {
    try {
        $("[name='baHgbaseinfo.specialRelation']").rules("add", {required: true});
        $("[name='baHgbaseinfo.priceEffect']").rules("add", {required: true});
        $("[name='baHgbaseinfo.paySpecialEnvoy']").rules("add", {required: true});
    } catch (e) {

    }
}

//========================海关公共END==============================///
